package com.hotel.front;

import java.io.IOException;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;

import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import com.Util;
import com.google.appengine.api.datastore.EntityNotFoundException;
import com.google.appengine.labs.repackaged.org.json.JSONException;
import com.google.gson.Gson;
import com.hotel.BL.POSCategoryLogic;
import com.hotel.BL.POSItemLogic;
import com.hotel.BL.POSLogic;
import com.hotel.BL.POSTableLogic;

import data.Counter;
import data.POS;
import data.passingData.ResultData;
@SuppressWarnings("serial")
public class POSService extends HttpServlet {

	public void doGet(HttpServletRequest req, HttpServletResponse resp)
			throws IOException {
		try {
			String func = req.getParameter("func");
			if (func.equals("getPOS")) {
				GetAllPOS(req, resp);
			}else if (func.equals("getFilterPOS")) {
				GetFilterPOS(req, resp);
			}else if (func.equals("getPOSPointById")) {
				GetPOSPointById(req, resp);
			}else if (func.equals("addPosPoint")) {
				AddPOSPoint(req, resp);
			}else if (func.equals("editPosPoint")) {
				EditPOSPoint(req, resp);
			}else if (func.equals("editStatusPosPoint")) {
				ChangeStatusPOSPoint(req, resp);
			}else if (func.equals("deletePosPoint")) {
				DeletePOSPoint(req, resp);
			}else if (func.equals("getFront")) {
				GetFront(req, resp);
			}else if(func.equals("getAllPOS")){
				GetAllPOSNolimit(req, resp);
			}
		} catch (Exception e) {
			e.printStackTrace();
		}
	}
	
	private void GetAllPOSNolimit(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);
		long hotelId = (Long) session.getAttribute("hotelId");
		String posId = req.getParameter("posId");
		POSLogic posLogic = new POSLogic(req,resp);
		Object pos;
		if(posId==null||posId.trim()=="")
			pos = posLogic.GetAllPOS(hotelId);
		else
			pos = posLogic.GetPOS(Long.parseLong(posId));
		Gson gson = new Gson();
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(pos));
	}
	
	private void GetAllPOS(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);	
		long hotelId = (Long) session.getAttribute("hotelId");
		long offset = Long.parseLong(req.getParameter("offset"));
		long line = Long.parseLong(req.getParameter("line"));
		long pointer = Long.parseLong(req.getParameter("pagePointer"));
		List<POS> obj = new POSLogic(req,resp).GetAllPOSLimit(hotelId,offset,line);
		POSCategoryLogic posCategoryLogic = new POSCategoryLogic(req,resp);
		POSTableLogic posTableLogic = new POSTableLogic(req,resp);
		List<HashMap<String,Object>> pos = new ArrayList<HashMap<String,Object>>();
		for(int i =0;i<obj.size();i++){
			int categorySize = posCategoryLogic.GetAllSubPOS(hotelId,obj.get(i).getId()).size();
			int productSize = new POSItemLogic(req,resp).GetAllPOSItemInPOS(hotelId,obj.get(i).getId()).size();
			int zoneSize = posTableLogic.GetAllPOSZonebyPosId(hotelId,obj.get(i).getId()).size();
			int counterSize =	obj.get(i).getCounterString().split(",").length;
			if(obj.get(i).getCounterString().equals(""))
				counterSize = 0;
			HashMap<String,Object> posSet = new HashMap<String,Object>();
			posSet.put("id",obj.get(i).getId());
			posSet.put("Title",obj.get(i).getTitle());
			posSet.put("avaliable",obj.get(i).isAvaliable());
			posSet.put("Description",obj.get(i).getDescription());
			posSet.put("pospointId",obj.get(i).getPospointId());
			posSet.put("product",obj.get(i).isProduct());
			posSet.put("productSize",productSize);
			posSet.put("zoneSize",zoneSize);
			posSet.put("counterSize", counterSize);
			posSet.put("table",obj.get(i).isTable());
			posSet.put("status",obj.get(i).isStatus());
			posSet.put("sizeCategory",categorySize);
			pos.add(posSet);
		}
		double dataSize = new POSLogic(req,resp).GetAllPOS(hotelId).size();
		Gson gson = new Gson();
		List<Object> rsObj = Arrays.asList(dataSize,pointer,line,pos);
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(rsObj));
	}
	
	private void GetFilterPOS(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);	
		long hotelId = (Long) session.getAttribute("hotelId");
		List<POS> obj = new POSLogic(req,resp).GetAllPOS(hotelId);
		Gson gson = new Gson();
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}
	
	private void GetPOSPointById(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		long id = Long.parseLong(req.getParameter("id"));
		POS obj = new POSLogic(req,resp).GetPOS(id);
		Gson gson = new Gson();
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}
	
	private void AddPOSPoint(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long hotelId = (Long) session.getAttribute("hotelId");
		String title = req.getParameter("title");
		String description = req.getParameter("description");
		String pospointId = req.getParameter("pospointId");
		Boolean table = Boolean.parseBoolean(req.getParameter("table"));
		Boolean product = Boolean.parseBoolean(req.getParameter("product"));
		String listcounter =req.getParameter("listcounter");
		String taxuse = req.getParameter("taxuse");
		String defaultcounter = req.getParameter("defaultcounter");
		Object obj = new POSLogic(req,resp).AddPOSPoint(hotelId,title,description,pospointId,table,product,listcounter,defaultcounter,taxuse);
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}
	
	private void EditPOSPoint(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long posPointId = Long.parseLong(req.getParameter("id"));
		String title = req.getParameter("title");
		String description = req.getParameter("description");
		String pospointId = req.getParameter("pospointId");
		Boolean table = Boolean.parseBoolean(req.getParameter("table"));
		Boolean product = Boolean.parseBoolean(req.getParameter("product"));
		String listcounter =req.getParameter("listcounter");
		String taxuse = req.getParameter("taxuse");
		String defaultcounter = req.getParameter("defaultcounter");
		Object obj = new POSLogic(req,resp).EditPOSPoint(posPointId,title,description,pospointId,table,product,listcounter,defaultcounter,taxuse);
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}
	
	private void ChangeStatusPOSPoint(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		long pospointId = Long.parseLong(req.getParameter("pospointId"));
		Object obj = new POSLogic(req,resp).ChangeStatusPosPoint(pospointId);
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}
	
	private void DeletePOSPoint(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{
		Gson gson = new Gson();
		String sessionCheck = Util.checkSessionEntity(req,resp);
		if(sessionCheck==null||sessionCheck.equals("")){
			resp.setContentType("text/plain; charset=UTF-8");
			resp.getWriter().println(gson.toJson(new ResultData(false)));
			return;
		}
		String unitIdList = req.getParameter("pospointId");
		for(int i = 0; i < unitIdList.split(",").length ; i++){
			new POSLogic(req,resp).DeletePOSPoint(Long.parseLong(unitIdList.split(",")[i]));
		}
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(true));
	}
	
	private void GetFront(HttpServletRequest req, HttpServletResponse resp) throws IOException, JSONException, ServletException, EntityNotFoundException
	{	
		HttpSession session=req.getSession(true);	
		long hotelId = (Long) session.getAttribute("hotelId");
		List<Counter> obj = new POSLogic(req,resp).GetPOS(hotelId,"iFront").getCounter();
		Gson gson = new Gson();
		resp.setContentType("text/plain; charset=UTF-8");
		resp.getWriter().println(gson.toJson(obj));
	}
}